package com.sicheng.lc.周赛.分类.模拟;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author zsc
 * @version 1.0
 * @date 2022/7/1 15:57
 */
public class 重排数字的最小值 {

    //https://leetcode.cn/problems/smallest-value-of-the-rearranged-number/

    List<Integer> nums = new ArrayList<>();
    boolean flag;

    public long smallestNumber(long num) {
        if (num == 0)
            return 0;
        flag = num > 0;
        num = Math.abs(num);
        while (num > 0) {
            nums.add((int) (num % 10));
            num /= 10;
        }
        Collections.sort(nums);
        long res = 0;
        if (flag) {
            int i = 0;
            for (; i < nums.size(); i++) {
                if (nums.get(i) > 0)
                    break;
            }
            Collections.swap(nums, 0, i);
            for (int x : nums) {
                res = res * 10 + x;
            }
            return res;
        }

        for (int i = nums.size() - 1; i >= 0; i--) {
            res = res * 10 + nums.get(i);
        }
        return -res;


    }

    public static void main(String[] args) {
        重排数字的最小值 s = new 重排数字的最小值();
        System.out.println(s.smallestNumber(310));
    }
}
